package com.itqf.service.impl;

import com.itqf.bean.DtsAdmin;
import com.itqf.service.*;
import com.itqf.vo.CategorySellVo;
import com.itqf.vo.OrderAmtsVo;
import com.itqf.vo.UserOrderCntVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/18 16:53
 * description:
 */
@Service
public class DtsDashboardServiceImpl implements DtsDashboardService {

    /**
     * 商品表
     */
    @Autowired
    private DtsGoodsService dtsGoodsService;

    /**
     * 库存表
     */
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;

    /**
     * 订单表
     */
    @Autowired
    private DtsOrderService dtsOrderService;

    /**
     * 用户表
     */
    @Autowired
    private DtsUserService dtsUserService;

    @Autowired
    private DtsRoleService dtsRoleService;

    @Autowired
    private DtsBrandService dtsBrandService;

    private final static Logger LOGGER = LoggerFactory.getLogger(DtsDashboardService.class);

    /**
     * 查询面板数据
     *
     * @return
     * @param dtsAdmin
     */
    @Override
    public Map<String, String> findNumber(DtsAdmin dtsAdmin) {

        //orderTotal ProductTotal GoodsTotal userTotal
        Map<String,String> data = new HashMap<>();

        //1.是不是管理员
        Integer[] roleIds = dtsAdmin.getRoleIds();

        if (roleIds == null || roleIds.length == 0)
        {
            LOGGER.warn("[DtsDashboardServiceImpl:findNumber]-->[用户没有分配角色]" +
                    "-->[未进行角色分配！]-->[进行赋权]-->[username:roleIds]:{}:null",dtsAdmin.getUsername());

            return data;
        }

        boolean isAdmin = dtsRoleService.isAdminRole(roleIds);

        //2.根据级别查询数据【管理员还是品牌制造商】

        if (isAdmin){

            Long goodsTotal =  dtsGoodsService.findGoodsTotal();
            Long productTotal =  dtsGoodsProductService.findProductTotal();
            Long orderTotal = dtsOrderService.findOrderTotal();
            Long userTotal = dtsUserService.findUserTotal();

            data.put("goodsTotal", goodsTotal+"");
            data.put("productTotal", productTotal+"");
            data.put("orderTotal", orderTotal+"");
            data.put("userTotal", userTotal+"");

        }else{

            //1.用户对应的品牌id
            List<Integer> brandIds = dtsBrandService.findBrandIdsByAdminId(dtsAdmin.getId());


            if (brandIds == null || brandIds.size() == 0) {

                LOGGER.warn("[DtsDashboardServiceImpl:findNumber]-->[品牌制造商没有指定品牌]" +
                        "-->[未进行品牌添加！]-->[品牌制造商添加品牌]-->[username]:{}",dtsAdmin.getUsername());

                return data;
            }

            //2.根据品牌查询
            Long goodsTotal =  dtsGoodsService.findGoodsTotal(brandIds);
            Long productTotal =  dtsGoodsProductService.findProductTotal(brandIds);
            Long orderTotal = dtsOrderService.findOrderTotal(brandIds);
            Long userTotal = 0L;

            data.put("goodsTotal", goodsTotal+"");
            data.put("productTotal", productTotal+"");
            data.put("orderTotal", orderTotal+"");
            data.put("userTotal", userTotal+"");
        }


        return data;
    }

    /**
     * 图表展示数据接口
     *
     * @return
     */
    @Override
    public Map<String, Object> findChart() {

        Map<String,Object> data = new HashMap<>();

        //1.查询元数据
        //类别的销售额
        CategorySellVo categorySellVo =  dtsOrderService.findOrderCategorySell();
        //查询订单的时间销售额
        OrderAmtsVo orderAmtsVo = dtsOrderService.findOrderSell();
        //用户订单数据
        UserOrderCntVo userOrderCntVo = dtsUserService.findUserAndOrderData(orderAmtsVo);

        //2.封装数据
        data.put("categorySell",categorySellVo);
        data.put("orderAmts", orderAmtsVo);
        data.put("userOrderCnt", userOrderCntVo);

        LOGGER.debug("[首页图表数据展示]-->[DtsDashboardServiceImpl:findChart]-->[data最终数据]:{}",data);

        return data;
    }
}
